package init.utils;

import init.config.Config;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Properties;

public class ConfigUtils {
	private static Properties config = null;

	private static File getConfigFile() throws URISyntaxException {
		String path = ConfigUtils.class.getResource("/").toURI().getPath();
		path += Config.DATABASE_PROPERTIES;
		return new File(path);
	}
	
	public static Properties parseConfig() throws IOException, URISyntaxException{
		if(config!=null && !config.isEmpty())
			return config;
		
		File file = getConfigFile();
		FileInputStream input = null;
		try{
			input = new FileInputStream(file);
			config = new Properties();
			config.load(input);
		}finally{
			if(input!=null)
				input.close();
		}
		return config;
	}
	
	public static boolean showSql(){
		try {
			String s = parseConfig().getProperty(Config.SHOW_SQL);
			return Boolean.parseBoolean(s.trim());
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
}
